import visitorId from '@/utils/fingerprint'

/**
 * 从服务器获取配置
 * @param {*} api
 * @returns
 */
export function fetchConfigure(api) {
  return new Promise((resolve, reject) => {
    fetch(api + '/api/watermark/threshold', {
      method: 'GET'
    })
      .then((res) => {
        resolve(res.json())
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * 上传设备信息
 * @param {*} devices
 * @returns
 */
export function UploadDeviceInfo(api, devices) {
  let data = new FormData()
  data.append('body', JSON.stringify(devices))
  fetch(api + '/api/device/save', {
    method: 'POST',
    body: data
  }).then(() => {
    localStorage.setItem('saveDevice', true)
  })
}

/**
 * 上传二维码图片
 * @param {*} api
 * @param {*} blob
 * @param {*} type
 * @param {*} value
 * @param {*} size
 * @returns
 */
export function UploadCodeImage(api, blob, type, markType, value, size) {
  return new Promise((resolve, reject) => {
    let data = new FormData()
    data.append('image', blob)
    data.append('body', JSON.stringify({ goodsClass: markType, deviceId: visitorId }))
    data.append('type', type)
    data.append('value', value || '')
    data.append('size', size || '')
    fetch(api + '/api/watermark/android_qr/up_load/take_picture', {
      method: 'POST',
      body: data
    })
      .then((res) => {
        resolve(res.json())
      })
      .catch((err) => {
        reject(err)
      })
  })
}

/**
 * 注册二维码图片
 * @param {*} api
 * @param {*} blob
 * @param {*} info
 * @returns
 */
export function RegisterCodeImage(api, blob, info) {
  return new Promise((resolve, reject) => {
    let data = new FormData()
    data.append('image', blob)
    data.append('body', info)
    fetch(api + '/api/watermark/register', {
      method: 'POST',
      body: data
    })
      .then((res) => resolve(res.json()))
      .catch((err) => {
        reject(err)
      })
  })
}
